package sparkcore.day7.lesson05

import java.util.Properties

import org.apache.spark.sql.{DataFrame, SQLContext}
import org.apache.spark.{SparkConf, SparkContext}

/**
  * Created by Administrator on 2018/5/3.
  */
object MysqlTest {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setMaster("local").setAppName("test")
    val sc = new SparkContext(conf)
    val sqlContext = new SQLContext(sc)
    /**
      * url : scala.Predef.String,
      *    URL="jdbc:mysql://hadoop1:3306/test"
      * table : scala.Predef.String,
      *   tableName=testsql
      * properties : java.util.Properties
      *   username
      *   password
      */
    val url="jdbc:mysql://hadoop1:3306/test"
    val table_name="testsql"
    val properties = new Properties()
    properties.put("user","aura")
    properties.put("password","aura")
    val df: DataFrame = sqlContext.read.jdbc(url,table_name,properties)

    df.createOrReplaceTempView("testsql")
    sqlContext.sql("select * from testsql").show()
  }

}
